package com.kkd.kkdapp.view;

import java.util.List;

import com.kkd.kkdapp.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

public class CycleViewPager extends ViewPager {

	private InnerPagerAdapter mAdapter;
	List<ImageView> dots;

	public void setDots(List<ImageView> dots) {
		this.dots = dots;
	}

	public CycleViewPager(Context context) {
		super(context);
		setOnPageChangeListener(null);
	}

	public CycleViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnPageChangeListener(null);
	}

	@Override
	public void setAdapter(PagerAdapter arg0) {
		mAdapter = new InnerPagerAdapter(arg0);
		super.setAdapter(mAdapter);
		setCurrentItem(1);
	}

	/*
	 * (non-Javadoc) 避免父控件拦截touch事件
	 */
	// @Override
	// public boolean dispatchTouchEvent(MotionEvent ev) {
	// ViewParent parent = getParent();
	// while(parent!=null){
	// parent.requestDisallowInterceptTouchEvent(true);
	// parent = parent.getParent();
	// }
	//
	// return super.dispatchTouchEvent(ev);
	// }
	float dertaY = 0;
	float lastY = 0;
	float dertaX = 0;
	float lastX = 0;

	/*
	 * (non-Javadoc) 避免父控件拦截touch事件
	 */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			dertaY = 0;
			lastY = ev.getY();
			dertaX = 0;
			lastX = ev.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			dertaY = ev.getY() - lastY;
			dertaX = ev.getX() - lastX;
			lastX = ev.getX();
			lastY = ev.getY();
			break;
		default:
			break;
		}

		if (Math.abs(dertaX) > Math.abs(dertaY)) {
			ViewParent parent = getParent();
			while (parent != null) {
				parent.requestDisallowInterceptTouchEvent(true);
				parent = parent.getParent();
			}
		}
		return super.dispatchTouchEvent(ev);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.ViewPager#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		try {
			super.onTouchEvent(arg0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return true;
	}

	@Override
	public void setOnPageChangeListener(OnPageChangeListener listener) {
		super.setOnPageChangeListener(new InnerOnPageChangeListener(listener));
	}

	private class InnerOnPageChangeListener implements OnPageChangeListener {

		private OnPageChangeListener listener;
		private int position;

		public InnerOnPageChangeListener(OnPageChangeListener listener) {
			this.listener = listener;
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			if (null != listener) {
				listener.onPageScrollStateChanged(arg0);
			}
			if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
				if (position == mAdapter.getCount() - 1) {
					setCurrentItem(1, false);
				} else if (position == 0) {
					setCurrentItem(mAdapter.getCount() - 2, false);
				}
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			if (null != listener) {
				listener.onPageScrolled(arg0, arg1, arg2);
			}
		}

		@Override
		public void onPageSelected(int arg0) {
			position = arg0;
			if (null != listener) {
				listener.onPageSelected(arg0);
			}

			if (dots == null)
				return;
			if (arg0 > 0 && arg0 < dots.size() + 1) {
				for (int i = 0; i < dots.size(); i++) {
					if (i == arg0 - 1) {
						dots.get(i).setBackgroundResource(
								R.drawable.page_focused);
					} else {
						dots.get(i).setBackgroundResource(
								R.drawable.page_unfocused);
					}
				}
			} else if (arg0 == dots.size() + 1) {
				for (int i = 0; i < dots.size(); i++) {
					if (i == 0) {
						dots.get(i).setBackgroundResource(
								R.drawable.page_focused);
					} else {
						dots.get(i).setBackgroundResource(
								R.drawable.page_unfocused);
					}
				}
			} else if (arg0 == 0) {
				for (int i = 0; i < dots.size(); i++) {
					if (i == dots.size() - 1) {
						dots.get(i).setBackgroundResource(
								R.drawable.page_focused);
					} else {
						dots.get(i).setBackgroundResource(
								R.drawable.page_unfocused);
					}
				}
			}
		}
	}

	private class InnerPagerAdapter extends PagerAdapter {

		private PagerAdapter adapter;

		public InnerPagerAdapter(PagerAdapter adapter) {
			this.adapter = adapter;
		}

		@Override
		public int getCount() {
			return adapter.getCount() + 2;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return adapter.isViewFromObject(arg0, arg1);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			if (position == 0) {
				position = adapter.getCount() - 1;
			} else if (position == adapter.getCount() + 1) {
				position = 0;
			} else {
				position -= 1;
			}
			return adapter.instantiateItem(container, position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			adapter.destroyItem(container, position, object);
		}

	}
}
